package com.pearadmin.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.pearadmin.hospital.domain.HospitalProblem;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.common.tools.secure.SecurityUtil;
import com.pearadmin.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.pearadmin.hospital.service.IHospitalProblemService;

import java.time.LocalDateTime;

/**
 * 问题药品Controller
 *
 * @author yuan
 * @date 2021-07-29
 */
@RestController
@RequestMapping("/hospital/problem")
public class HospitalProblemController extends BaseController
{
    private String prefix = "hospital/problem";

    @Autowired
    private IHospitalProblemService hospitalProblemService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/hospital/problem/main','hospital:problem:main')")
    public ModelAndView main()
    {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询问题药品列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/hospital/problem/data','hospital:problem:data')")
    public ResultTable list(@ModelAttribute HospitalProblem hospitalProblem, PageDomain pageDomain)
    {
        PageInfo<HospitalProblem> pageInfo = hospitalProblemService.selectHospitalProblemPage(hospitalProblem,pageDomain);
        return pageTable(pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 新增问题药品
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/hospital/problem/add','hospital:problem:add')")
    public ModelAndView add()
    {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存问题药品
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/hospital/problem/add','hospital:problem:add')")
    public Result save(@RequestBody HospitalProblem hospitalProblem)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        hospitalProblem.setCreateTime(LocalDateTime.now());
        hospitalProblem.setCreateBy(sysUser.getUserId());
        hospitalProblem.setCreateName(sysUser.getUsername());
        return decide(hospitalProblemService.insertHospitalProblem(hospitalProblem));
    }

    /**
     * 修改问题药品
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/hospital/problem/edit','hospital:problem:edit')")
    public ModelAndView edit(Integer id, ModelMap mmap)
    {
        HospitalProblem hospitalProblem = hospitalProblemService.selectHospitalProblemById(id);
        mmap.put("hospitalProblem", hospitalProblem);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存问题药品
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/hospital/problem/edit','hospital:problem:edit')")
    public Result update(@RequestBody HospitalProblem hospitalProblem)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        hospitalProblem.setUpdateTime(LocalDateTime.now());
        hospitalProblem.setUpdateBy(sysUser.getUserId());
        hospitalProblem.setUpdateName(sysUser.getUsername());
        return decide(hospitalProblemService.updateHospitalProblem(hospitalProblem));
    }

    /**
     * 删除问题药品
     */
    @ResponseBody
    @DeleteMapping( "/batchRemove")
    @PreAuthorize("hasPermission('/hospital/problem/remove','hospital:problem:remove')")
    public Result batchRemove(String ids)
    {
        return decide(hospitalProblemService.deleteHospitalProblemByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/hospital/problem/remove','hospital:problem:remove')")
    public Result remove(@PathVariable("id") Integer id)
    {
        return decide(hospitalProblemService.deleteHospitalProblemById(id));
    }
}
